package cn.tedu.knows.search.service.impl;


import cn.tedu.knows.commons.model.Tag;
import cn.tedu.knows.commons.model.User;
import cn.tedu.knows.search.VO.QuestionVO;
import cn.tedu.knows.search.repository.QuestionRepository;
import cn.tedu.knows.search.service.IQuestionService;
import cn.tedu.knows.search.utils.Pages;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class QuestionServiceImpl implements IQuestionService {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private QuestionRepository questionRepository;

    @Override
    public void syncData() {
        // 先使用Ribbon调用获得总页数的rest接口
        String url =
                "http://faq-service/v2/questions/page/count?pageSize={1}";
        int pageSize = 8;
        Integer total = restTemplate.getForObject(
                url, Integer.class, pageSize);
        //循环调用分页查询所有问题的方法
        for (int i = 1; i <= total; i++) {
            url = "http://faq-service/v2/questions/" +
                    "page?pageNum={1}&pageSize={2}";
            // 查询出第i页的数据
            QuestionVO[] questions = restTemplate.getForObject(
                    url, QuestionVO[].class, i, pageSize);
            // 将查询出来的问题新增到ES
            questionRepository.saveAll(Arrays.asList(questions));
            log.debug("完成了第{}页的新增", i);
        }

    }

    @Override
    public PageInfo<QuestionVO> search(String key, String username,
                                       Integer pageNum, Integer pageSize) {
        // 获得用户信息
        String url = "http://sys-service/v1/auth/user?username={1}";
        User user = restTemplate.getForObject(
                url, User.class, username);
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize,
                Sort.Direction.DESC, "createtime");
        Page<QuestionVO> page = questionRepository
                .queryAllByParams(key, key, user.getId(), pageable);
        // 根据每个问题的tagNames属性为它的Tags属性赋值
        for (QuestionVO vo : page) {
            vo.setTags(tagNamesToTags(vo.getTagNames()));
        }
        // 返回时将page转换为pageInfo返回
        return Pages.pageInfo(page);
    }

    @Override
    public void saveQuestion(QuestionVO questionVO) {
        questionRepository.save(questionVO);
    }

    // 根据tagNames属性返回对应的标签集合
    private List<Tag> tagNamesToTags(String tagNames) {
        // java基础,javaSE,面试题
        String[] names = tagNames.split(",");
        // 下面是Ribbon调用获得所有标签
        String url = "http://faq-service/v2/tags";
        Tag[] tagArr = restTemplate.getForObject(url, Tag[].class);
        // 创建一个Map,将所有标签赋值给它
        Map<String, Tag> map = new HashMap<>();
        for (Tag t : tagArr) {
            map.put(t.getName(), t);
        }
        List<Tag> tags = new ArrayList<>();
        for (String name : names) {
            tags.add(map.get(name));
        }
        return tags;

    }


}
